package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResuit;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {


    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelEsService travelEsService;

    @Reference
    private IDestinationEsService destinationEsService;

    @Reference
    private IStrategyEsService strategyEsService;

    @Reference
    private IUserInfoEsService userInfoEsService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(Model model){
        // tcs
        model.addAttribute("tcs", travelCommendService.getTop5());

        // sc
        model.addAttribute("sc", strategyCommendService.getTop5().get(0));


        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "index/travelPageTpl";
    }


    @RequestMapping("/q")
    public String seach(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return this.searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return this.searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return this.searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return this.searchUser(model, qo);
            default:
                return this.searchAll(model, qo);
        }
    }

    // 所有搜索
    private String searchAll(Model model, SearchQueryObject qo) {

        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");

        SearchResuit searchResuit = new SearchResuit();

        searchResuit.setUsers(us.getContent());
        searchResuit.setTravels(ts.getContent());
        searchResuit.setStrategys(sts.getContent());
        searchResuit.setDests(ds.getContent());

        model.addAttribute("data", searchResuit);

        return "index/searchAll";
    }

    // 用户搜索
    private String searchUser(Model model, SearchQueryObject qo) {

        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");

        model.addAttribute("page", us);

        return "index/searchUser";
    }

    // 游记搜索
    private String searchTravel(Model model, SearchQueryObject qo) {

        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page", ts);

        return "index/searchTravel";
    }

    // 攻略搜索
    private String searchStrategy(Model model, SearchQueryObject qo) {

        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");

        model.addAttribute("page", sts);

        return "index/searchStrategy";
    }

    // 目的地搜索
    // 需求: 判断keyword 是不是一个目的地, 如果不是提示不是目的地, 如果是, 查询
    //       该目的地下所有的攻略, 游记, 用户
    private String searchDest(Model model, SearchQueryObject qo) {
        // 1:确定keyword是否是目的地
        DestinationTemplate dest = destinationEsService.findByName(qo.getKeyword());
        if(dest != null){
            // 2:如果是查询该目的地下攻略, 游记, 用户
            List<StrategyTemplate> sts = strategyEsService.findByDestName(dest.getName());
            List<TravelTemplate> ts = travelEsService.findByDestName(dest.getName());
            List<UserInfoTemplate> us = userInfoEsService.findByDestName(dest.getName());

            SearchResuit result = new SearchResuit();

            result.setStrategys(sts);
            result.setTravels(ts);
            result.setUsers(us);
            long total = sts.size() + ts.size() + us.size();

            result.setTotal(total);

            // 共享的是一个data数据
            model.addAttribute("data", result);
        }
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }
}
